<template>

  <!-- 轮播图 -->
  <div class="carousel-container">
    <el-carousel height="400px" indicator-position="outside">
      <el-carousel-item v-for="item in carouselImages" :key="item">
        <img :src="item" alt="轮播图" class="carousel-image" />
      </el-carousel-item>
    </el-carousel>
  </div>

  <!-- 功能卡片 --->
  <div class="features">
    <el-card class="feature-card" @click="goToSongs">
      <template #header>
        <div class="card-header">
          <span>热门歌曲</span>
        </div>
      </template>
      <div class="card-content">
        发现平台最受欢迎的歌曲
      </div>
    </el-card>

    <el-card class="feature-card" @click="goToSingers">
      <template #header>
        <div class="card-header">
          <span>歌手列表</span>
        </div>
      </template>
      <div class="card-content">
        查看你喜欢的歌手作品
      </div>
    </el-card>

    <el-card class="feature-card" @click="goToAlbums">
      <template #header>
        <div class="card-header">
          <span>专辑列表</span>
        </div>
      </template>
      <div class="card-content">
        浏览最新最热的音乐专辑
      </div>
    </el-card>

    <el-card class="feature-card" @click="goToRanks">
      <template #header>
        <div class="card-header">
          <span>排行榜</span>
        </div>
      </template>
      <div class="card-content">
        查看最新音乐排行榜
      </div>
    </el-card>
  </div>
</template>

<script>
import { useRouter, useRoute } from 'vue-router'
import { useUserStore } from '@/store/user'
import { ElMessage } from 'element-plus'
import { onMounted, ref, watch } from 'vue'

export default {
  name: 'Home',
  setup() {
    const router = useRouter()
    const route = useRoute()
    const userStore = useUserStore()
    const activeMenu = ref('/')
    const searchKeyword = ref('')

    const goToLogin = () => {
      router.push('/login')
    }

    const goToRegister = () => {
      router.push('/register')
    }

    const handleLogout = () => {
      userStore.logout()
      ElMessage.success('已退出登录')
    }

    const handleMenuSelect = (index) => {
      router.push(index)
    }

    const goToSongs = () => {
      router.push('/songs')
    }

    const goToSingers = () => {
      router.push('/singers')
    }

    const goToAlbums = () => {
      router.push('/albums')
    }

    const goToRanks = () => {
      router.push('/ranks')
    }

    const goToProfile = () => {
      router.push('/profile')
    }

    const goToSongLists = () => {
      router.push('/songlists')
    }

    const goToMySongLists = () => {
      router.push('/songlists')
    }

    const goToHistory = () => {
      router.push('/history')
    }

    const goToRecent = () => {
      router.push('/recent')
    }

    const performSearch = () => {
      if (searchKeyword.value.trim()) {
        router.push(`/search?keyword=${encodeURIComponent(searchKeyword.value)}`)
      }
    }

    const carouselImages = ref([
      '/carousel/104.png',
      '/carousel/103.png',
      '/carousel/102.png',
      '/carousel/101.png',
      '/carousel/100.png'
    ])

    // 页面加载时如果已登录则获取用户信息
    onMounted(async () => {
      if (userStore.isLoggedIn) {
        try {
          await userStore.fetchUserInfo()
        } catch (error) {
          console.error('获取用户信息失败:', error)
        }
      }
    })

    // 监听路由变化，更新激活的菜单项
    watch(
        () => route.path,
        (newPath) => {
          if (newPath.startsWith('/singer/')) {
            activeMenu.value = '/singers'
          } else if (newPath.startsWith('/album/')) {
            activeMenu.value = '/albums'
          } else if (newPath.startsWith('/rank/')) {
            activeMenu.value = '/ranks'
          } else if (newPath.startsWith('/songlist/')) {
            activeMenu.value = '/songlists'
          } else {
            activeMenu.value = newPath
          }
        },
        { immediate: true }
    )

    return {
      userStore,
      activeMenu,
      searchKeyword,
      carouselImages,
      goToLogin,
      goToRegister,
      handleLogout,
      handleMenuSelect,
      goToSongs,
      goToSingers,
      goToAlbums,
      goToRanks,
      goToProfile,
      goToSongLists,
      goToMySongLists,
      goToHistory,
      goToRecent,
      performSearch
    }
  }
}
</script>

<style scoped>
.features {
  display: flex;
  justify-content: center;
  gap: 20px;
  flex-wrap: wrap;
}

.feature-card {
  width: 250px;
  cursor: pointer;
}

.feature-card:hover {
  transform: translateY(-5px);
  transition: transform 0.3s ease;
}

.card-header {
  font-weight: bold;
  font-size: 16px;
}

.card-content {
  color: #666;
  font-size: 14px;
}

.carousel-container {
  margin-bottom: 30px;
  border-radius: 8px;
  overflow: hidden;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
}

.carousel-image {
  width: 100%;
  height: 400px;
  object-fit: cover;
}
</style>